Constant
Constant <.Name.> = StringValue|FloatValue|IntegerValue
 
Parameters: NONE
Returns: NONE
 

      The Constant directive allows you to declare your own constant values. While constants may appear to be the same as variables on the surface, there is one key difference.

      Unlike variables, who's value can't be changed after they are defined, so once a contant is declared it can not be changed.




FACTS:


     * PlayBASIC will pre-evaluate constant expressions where possible (operations between constants), as well as some built in core functions.

      * Constants can be used with #IF/#ELSE/#ENDIF directives

      * Constants can only assign to expressions that resolve to be constant, since their are resolved at compile time, not run time.





Mini Tutorial #1:


      This simple examples shows how to declare some Integer, Float# and String$ constants.

      You'll noticed that some those declarations are assigning expressions (formulas), this is only possible when the expressions are solely made up constants and or pre-solvable functions. Most of the core functions in PlayBASIC are solvable at compile time, providing they are given constant parameters, but not all.


  
  
; Define some floating point constants
  Constant Gravity#=9.8
  Constant Version#=123.456
  
; Define an integer constant
  Constant IntegerVersionBy2=Version#*2
  
; Define some string constants
  Constant Name$="Play Basic"
  Constant VersionName$=Name$+"  V"+Str$(Version#)
  
; Display these constant values
  Print Name$
  Print Version#
  Print IntegerVersionBy2
  Print VersionName$
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  



This example would output.


  
  Play Basic
  123.456
  247
  PlayBASIC   V123.456
  






Mini Tutorial #2:


      In this example we're using a constant to scale a series of values in some data statements by using pre-evaluation features of PlayBasic. What happens, is PB will resolve the expressions in the data statements during the compile. So it only stores the result of each calculation, not the code calculation code itself. Now it only do this with expressions that working upon constant / literal values.


  
  
  Constant Scaler#=1.25
  
  Data 1*Scaler#
  Data 2*Scaler#
  Data 3*Scaler#
  Data 4*Scaler#
  Data 5*Scaler#
  
  
  For lp=1 To 5
     Print ReadData#()
  Next
  Sync
  WaitKey
  
  
  



  
  1.25
  2.5
  3.75
  5.0
  6.25
  



 
Related Info: #IF | AC | Dim | Explicit | Global | Literals | Local | Static | Variables :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com